package com.game.service.app.get_score;

import com.game.service.app.PostJson;
import com.game.service.db.UserScoreDaoImpl;
import com.game.service.socket.FileUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import static com.game.service.app.Score.doPostForm;
import static com.game.service.app.Utils.LogDate;
import static com.game.service.define.RoomConfig.*;

public class PostJsonDealFailed {

    static PostJsonDealFailed instance;

    private ISendListener mISendListener;

    public void addListener(ISendListener mISendListener) {
        this.mISendListener = mISendListener;
    }

    public static PostJsonDealFailed getInstance() {
        if (instance == null) {
            instance = new PostJsonDealFailed();
        }
        return instance;
    }

    private void post(PostJson json, int maxPost) {
        if (json.getPostResult() == 1) {//等于1时表示已经处理成功
            return;
        }
        if (maxPost-- == 0) {
            FileUtils.writeTxtToFile("发送失败,等待下次发送" + json, LogDate(), LOG_NAME_FAIL_JSON);
            return;
        }

        int finalMaxPost = maxPost;
        new Thread(() -> {
            String result = doPostForm(json.getPostUrl(), json.getPostJson());
            String log = "发送结果,第" + (EXIT_MAX_POST - finalMaxPost) + "次,id=" + json.getPid() + "返回=" + result;
            FileUtils.writeTxtToFile(log, LogDate(), LOG_NAME_FAIL_JSON);
            //  {"code":0,"msg":"ok","data":""}
            System.out.println("PostJsonDeal," + log);
            if (result.isEmpty()) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                post(json, finalMaxPost);
                return;
            }
            Type type0 = new TypeToken<JsonBean>() {
            }.getType();
            JsonBean bean = new Gson().fromJson(result, type0);
            if (bean != null && bean.code == 0) {

                UserScoreDaoImpl.getInstance().updateLog(json.getPid());
                if (mISendListener != null) {
                    mISendListener.sendSuccess();//发送成功
                }

                FileUtils.writeTxtToFile("数据发送,发送成功:" + json, LogDate(), LOG_NAME_POST_JSON);

            }

        }).start();
    }


    public void dealFailJson() {
        long current = System.currentTimeMillis();
        //去数据里查询==0的未发送的数据进行清零
        List<PostJson> dbList = UserScoreDaoImpl.getInstance().getList(GAME_ID);
        if (dbList.isEmpty()) {
            // System.out.println("全部发送成功");
            return;
        }

        for (PostJson postJson : dbList) {

            if (postJson.getPostResult() == 0) {
                //防止重复发送,必须间隔3000ms
                long time = current - postJson.getAddCurrentTimeMillis();
                if (time > 3000) {
                    System.out.println("间隔时间=" + time + ",发送=" + postJson);
                    post(postJson, EXIT_MAX_POST);
                }

            }

        }
    }

    public interface ISendListener {
        void sendSuccess();

        void error(String e);
    }

}
